
/*
This file is part of MJIN2.

Copyright (C) 2012-2013 Opensource Game Studio

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#ifndef MJIN2_PRIV_SEQUENCE_H
#define MJIN2_PRIV_SEQUENCE_H

#include <mjin2/Common.h>
#include <mjin2/Sequence.h>
#include <mjin2/TimerListener.h>

#include <map>

namespace mjin2
{
class SequenceListener;
class Timer;

namespace priv
{

class Sequence : public TimerListener
{
        DISALLOW_COPY_AND_ASSIGN(Sequence);
    public:
        Sequence(mjin2::Timer *timer);
        virtual ~Sequence();

        void addPoint(const SequencePoint &p, u32 reachTime);
        void clear();
        void runInOneStep();
        void setInterpolation(SEQUENCE_INTERPOLATION si);
        void setListener(SequenceListener *listener);
        void start();
        void stop();

    private:
        inline bool canContinue();
        inline float linearInterpolate(float p1, float p2, float t);
        SequencePoint linearInterpolate(const SequencePoint &p1,
                                        const SequencePoint &p2,
                                        float t);
        virtual void onTimerTick(u32 elapsedMilliseconds);
        void prepare();
        inline float splineInterpolate(float p0,
                                       float p1,
                                       float p2,
                                       float p3,
                                       float t);
        SequencePoint splineInterpolate(const SequencePoint &p0,
                                        const SequencePoint &p1,
                                        const SequencePoint &p2,
                                        const SequencePoint &p3,
                                        float t);

        struct Point
        {
            Point(const SequencePoint &p, u32 rt) : p(p), rt(rt) { }

            SequencePoint p;
            u32 rt;
        };
        typedef std::vector<Point> Points;
        typedef std::map<u32, s32> TimeToID;

        mjin2::Timer *mTimer;
        bool mIsRunning;
        SEQUENCE_INTERPOLATION mSI;
        Points mPoints;
        Points mAllPoints;
        SequenceListener *mListener;
        u32 mElapsedSinceLastPoint;
        u32 mCurrentPointID;
        u32 mLastPointID;
};

} // namespace priv
} // namespace mjin2

#endif // MJIN2_PRIV_SEQUENCE_H

